<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2024/12/18
 * 描述:
 */

namespace app\result;

use app\model\user\UserAddress;

class AddressResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new UserAddress();
    }

    public function list($user_id, $args = [])
    {
        $data = $this->model->data_list_nopage(['user_id' => $user_id], 'address_id,name,phone,province,city,area,detail,is_default', 'is_default desc,address_id DESC');
        if ($data) {
            foreach ($data as &$item) {
                $item['address_id'] = strval($item['address_id']);
            }
            unset($item);
        }
        return self::success_return('success', [
            'list' => $data
        ]);
    }

    public function action($user_id, $args = [])
    {
        $address_id = intval($args['address_id'] ?? 0);
        if ($address_id) {
            $data = $this->model->data_get(['user_id' => $user_id, 'address_id' => $address_id]);
            if (!$data) {
                return self::error_return('地址不存在');
            }
        }

        $rules = [
            ['name', 'empty|max:32', '请填写收货人姓名|收货人不能大于30个字'],
            ['phone', 'empty|length:11', '请填写手机号码|手机号码不正确'],
            ['region', 'empty', '请选择省市区'],
            ['detail', 'empty', '请填写详细地址']
        ];
        $check = check_param($args, $rules);
        if ($check['status'] != 0) {
            return self::error_return($check['msg']);
        }
        [$province, $city, $area] = explode('/', trim($args['region']));

        $baseData = [
            'user_id' => $user_id,
            'name' => $args['name'],
            'phone' => $args['phone'],
            'province' => $province,
            'city' => $city,
            'area' => $area,
            'detail' => trim($args['detail']),
            'is_default' => !empty($args['is_default']) ? 1 : 0
        ];
        if ($args['is_default']) {
            $this->model->data_update(['user_id' => $user_id], ['is_default' => 0]);
        }
        if ($address_id) {
            $this->model->data_update(['address_id' => $address_id], $baseData);
        } else {
            $this->model->data_add($baseData);
        }
        return self::success_return('保存成功');
    }

    public function get($user_id, $args = [])
    {
        $address_id = intval($args['address_id'] ?? 0);
        if (!$address_id) {
            return self::error_return('参数错误');
        }
        $data = $this->model->data_get(['user_id' => $user_id, 'address_id' => $address_id]);
        if (!$data) {
            return self::error_return('地址不存在');
        }
        $data['region'] = $data['province'] . '/' . $data['city'] . '/' . $data['area'];
        $data['default_region'] = [$data['province'], $data['city'], $data['area']];
        return self::success_return('success', $data);
    }

    public function setDefault($user_id, $args = [])
    {
        $address_id = intval($args['address_id'] ?? 0);
        if (!$address_id) {
            return self::error_return('参数错误');
        }
        $data = $this->model->data_get(['user_id' => $user_id, 'address_id' => $address_id]);
        if (!$data) {
            return self::error_return('地址不存在');
        }
        $this->model->data_update(['user_id' => $user_id], ['is_default' => 0]);
        $this->model->data_update(['address_id' => $address_id], ['is_default' => 1]);
        return self::success_return('设置成功');
    }

    public function delete($user_id, $args = []): array
    {
        $address_id = intval($args['address_id'] ?? 0);
        if (!$address_id) {
            return self::error_return('参数错误');
        }
        $data = $this->model->data_get(['user_id' => $user_id, 'address_id' => $address_id]);
        if (!$data) {
            return self::error_return('地址不存在');
        }
        $this->model->data_del(['address_id' => $address_id]);
        return self::success_return('操作成功');
    }
}
